// pages/user/user.js
var app = getApp()
Page({

  data: {
    username1: null,
    password1: null,
    username2: null,
    password2: null,
  },
  login: function (e) {
    var that = this;
    var name = e.detail.value.user;
    var pwd = e.detail.value.password;
    console.log(name, pwd)
    if (!name) {
      wx.showModal({
        title: '',
        content: '用户名不可为空！',
      })
    } else if (!pwd) {
      wx.showModal({
        title: '',
        content: '密码不可为空！',
      })
    } else {
      wx.request({
        url: 'http://localhost/shopping/admin_users.php',
        header: {
          'Content-Type': 'application/json'
        },
        data: {},
        success: function (res) {
          // console.log(res.data);
          wx.request({
            url: 'http://localhost/shopping/admin_admin.php',
            method: 'GET',
            header: {
              'Content-Type': 'application/json'
            },
            data: {},
            success: function (res) {
              // console.log(res.data);
              for (var i = 0; i < res.data.admin.length; i++) {
                that.username2 = res.data.admin[i].adminname,
                  that.password2 = res.data.admin[i].adminpwd
                // console.log(that.username2,that.password2)
              }
              if (name == that.username2 && pwd == that.password2) {
                console.log('管理员登陆成功')

                wx.navigateTo({
                  url: '/pages/admin/admin',
                })
                return;
              } else {
                console.log('管理员登录失败')
              }
              that.setData({})
            },
            fail: function (res) {
              console.log("fail");
            }
          })
          for (var i = 0; i < res.data.shopping.length; i++) {
            that.username1 = res.data.shopping[i].username,
              that.password1 = res.data.shopping[i].password
            // console.log(that.username1, that.password1)

            if (name == that.username1 && pwd == that.password1) {
              console.log('用户登陆成功')
              //把参数保存至全局变量
              // console.log(name)
              getApp().globalData.usersname = name
              wx.switchTab({
                url: '/pages/goods/goods',
              })
              return;
            } else {
              console.log('用户登录失败')
            }
          }

          that.setData({})
        },
        fail: function (res) {
          console.log("fail");
        }
      })
    }

  },

  modal_add: function (e) {
    wx.navigateTo({
      url: '/pages/login-new/login-new',
    })
  },
  modal_help() {
    wx.showModal({
      title: '忘记密码',
      content: '是否向管理员发送请求',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

})